<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Area;
use app\common\model\User;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\View;
use think\Response;

class Register extends Base
{

    /**
     * @return Response
     * 原点 2022年12月22日15:34:16 注册
     */
    public function add(): Response
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Register');
            return Result::fastReturn(User::_save($param));
        } catch (\Exception $e) {
            return Result::fail($e->getError());
        }

    }

    public function web(): string
    {
        $code = $this->request->param('code','');
        View::assign([
            'code' => $code
        ]);
        return View::fetch();
    }

    public function agreement(): string
    {
        $type = 'register_msg';
        $title = ['register_msg' => '注册协议'];

        View::assign([
            'title' => $title[$type],
            'content' => Cache::get('system')[$type]
        ]);
        return View::fetch();
    }

    public function getArea($id, $level = 1)
    {
        return Area::getArea($id ?: 100000, $level);
    }
}
